﻿using System.Collections.Generic;
using System.Linq;

namespace Roulette
{
    public class BestOddsInvariableBetAlgorithm : IAlgorithm
    {
        public double Amount { get; set; }
        public Bet Bet { get; set; }

        public BestOddsInvariableBetAlgorithm()
        {
            this.Amount = 1d;
        }

        public Bet ChooseBet(Outcome outcome, IEnumerable<Option> options)
        {
            if (ReferenceEquals(null, this.Bet))
            {
                var positiveOutcomes = options
                    .SelectMany(a => a.Outcomes)
                    .Where(a => a.Gain > 0)
                    ;
                var bestOddsPositiveOutcome = positiveOutcomes
                    .Where(a => a.Probability == positiveOutcomes.Max(b => b.Probability))
                    .First();
                this.Bet = new Bet()
                {
                    Amount = this.Amount
                    ,
                    Option = options
                        .Where(option => option.Outcomes.Contains(bestOddsPositiveOutcome))
                        .First()
                };
            }
            return this.Bet;
        }
    }
}
